{% extends 'extends/nav_front_all_.html' %}

{% block main %}
<div class="container py-5">
    <!-- 点赞管理卡片 -->
    <div class="card mb-4">
        <div class="card-header">
            <h4>点赞管理</h4>
        </div>
        <div class="card-body">
            <div class="container mt-4">
                <div class="row">
                    <div class="col-md-12">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>帖子</th>
                                    <th>日期</th>
                                    <th>取消点赞</th>
                                </tr>
                            </thead>
                            <tbody id="likes-body">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 关注管理卡片 -->
    <div class="card">
        <div class="card-header">
            <h4>关注管理</h4>
        </div>
        <div class="card-body">
            <div class="container mt-4">
                <div class="row">
                    <div class="col-md-12">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>用户ID</th>
                                    <th>关注时间</th>
                                    <th>取消关注</th>
                                </tr>
                            </thead>
                            <tbody id="follows-body">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>

        function UnlikeEvent(url,pid){
        Fetch(url,'delete',{pid:pid},async res => {
            await Flash(res.msg)
            Redirect()
        })
    }

            function UnfollowEvent(url,uid){
        Fetch(url,'delete',{uid:uid},async res => {
            await Flash(res.msg)
            Redirect()
        })
    }

onUrlsReady(urls => {
    // 获取点赞列表数据
    Fetch(urls.info.mylikes, 'get', {}, res => {
        if (res.status === 'success') {
            res.data.likes.forEach(like => {
                const post_url = urls.page.front.blogdetail.replace('0', like.pid);
                const but = `<a class="btn btn-sm btn-danger"
                                    data-pid="${like.pid}"
                                    onclick="UnlikeEvent('${urls.service.likeorcancel}', ${like.pid})">
                                    取消点赞
                                </a>`;
                const row = table_item(
                        `<a href="${post_url}">${like.pid}</a>`,
                        like.time,
                        but
                    );

                Element_('likes-body').insertAdjacentHTML('beforeend', row);
            });
        }
    });

    // 获取关注列表数据
    Fetch(urls.info.myfollows, 'get', {}, res => {
        if (res.status === 'success') {
            res.data.follows.forEach(follow => {
                const user_url = urls.page.front.personaldetail.replace('0', follow.uid);
                const but = `<a class="btn btn-sm btn-danger"
                                    onclick="UnfollowEvent('${urls.service.followorcancel}', ${follow.uid})">
                                    取消关注
                                </a>`;

                const row = table_item(
                    `<a href="${user_url}">${follow.uid}</a>`,
                    follow.time,
                    but
                );
                Element_('follows-body').insertAdjacentHTML('beforeend', row);

            });
        }
    });
});
</script>

{% endblock %}